package Other;

import java.io.IOException;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * @Classname Ping
 * @Description TODO
 * @Date 2021/5/7 15:41
 * @Created by ASUS
 */


public class Ping {

    public static void main(String[] args) throws IOException, InterruptedException {
        System.out.println("请输入待Ping的地址或者域名：");
        Scanner scanner = new Scanner(System.in);
        String addr = scanner.nextLine();
        scanner.close();
        InetAddress address = InetAddress.getByName(addr);
        System.out.println("正在Ping " + addr + " [" + address.getHostAddress() + "] 具有32字节的数据");
        int flag = 0;
        for (int i = 0; i < 4; i++) {
            boolean b = address.isReachable(1000);
            System.out.println("来自  " + address.getHostAddress() + " 的回复:  " + (b ? "成功" : "失败"));
            if (b)
                flag++;
            Thread.sleep(1000);
        }
        System.out.println();
        System.out.println(address.getHostAddress() + " 的  Ping 统计信息：");
        System.out.println("    数据包：已发送 = 4， 已接收 = " + flag + " ,丢失 = " + (4 - flag) + "(" + (4 - flag) / 4 * 100 + "% 丢失)");
    }

}
